Skip to main content

Click Scripts

Overview

Command Line Interface Creation Kit (Click) Scripts.

According to documentation

''' Click in three points:

  • arbitrary nesting of commands
  • automatic help page generation
  • supports lazy loading of subcommands at runtime '''

It creates a package based on the python files.

'''

pip install click

'''

django-click

Since we are using django we may want to look at django click. This will take the manage.py commands and run using click scripts. Since we have already migrated the scripts into commands in the Django framework this would be a logical choice.

'''

pip install django-click

'''

According to Chat GPT:

''' Certainly! Below is an example of a Click script for a Django project. In this example, we'll create a command-line interface that interacts with the Django project to perform tasks like running the development server, creating superusers, and managing database migrations.

Before using this script, ensure you have Click and Django installed:

pip install click django
import os
import click
from django.core.management import call_command

@click.group()
def cli():
"""Django CLI"""
pass

@cli.command()
@click.option('--port', default=8000, help='Port number for the development server.')
def runserver(port):
"""Run the Django development server."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
call_command('runserver', f'0.0.0.0:{port}')

@cli.command()
@click.option('--username', prompt=True, help='Superuser username.')
@click.option('--email', prompt=True, help='Superuser email.')
@click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True, help='Superuser password.')
def createsuperuser(username, email, password):
"""Create a new superuser."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
call_command('createsuperuser', username=username, email=email)

@cli.command()
def makemigrations():
"""Create new database migrations."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
call_command('makemigrations')

@cli.command()
def migrate():
"""Apply database migrations."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
call_command('migrate')

if __name__ == '__main__':
cli()

In this example, we use the click.group() decorator to define a group of commands under the cli function. Each individual command is defined as a function using the @cli.command() decorator.

The commands provided in this example are:

  1. runserver: Starts the Django development server on the specified port (default is 8000).

  2. createsuperuser: Creates a new superuser with the provided username, email, and password.

  3. makemigrations: Creates new database migrations based on the changes in your Django models.

  4. migrate: Applies database migrations to update the database schema.

To use this Click script, save it to a Python file, for example, django_cli.py, and then run it from the command line using Python:

python django_cli.py runserver
python django_cli.py createsuperuser
python django_cli.py makemigrations
python django_cli.py migrate

Replace your_project.settings with the actual path to your Django project's settings module. With this Click script, you can conveniently manage your Django project through the command line. '''

Project Documentation website: https://click.palletsprojects.com/en/8.1.x/

Read the docs: https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html#the-console-scripts-entry-point

Click Django: https://github.com/GaretJax/django-click